package game.module.mj.bean;

import game.module.mj.MjConstants;

public class CChiHuRight {
	private static boolean m_bInit;
	private static int[] m_dwRightMask;
	private int m_dwRight[];

	public CChiHuRight() {
		m_bInit = false;
		m_dwRight = new int[MjConstants.MAX_RIGHT_COUNT];
		m_dwRightMask = new int[MjConstants.MAX_RIGHT_COUNT];
		if (!m_bInit) {
			m_bInit = true;
			for (byte i = 0; i < m_dwRightMask.length; i++) {
				if (0 == i)
					m_dwRightMask[i] = 0;
				else
					m_dwRightMask[i] = ((int) (Math.pow(2, i - 1))) << 28;
			}
		}
	}

	// 赋值 ==
	public void Fuzhi(int dwRight) {
		int dwOtherRight = 0;
		// 验证权位
		if (!IsValidRight(dwRight)) {
			// 验证取反权位
			assert (IsValidRight(~dwRight));
			if (!IsValidRight(~dwRight))
				// return *this;
				return;
			dwRight = ~dwRight;
			dwOtherRight = MjConstants.MASK_CHI_HU_RIGHT;
		}

		for (byte i = 0; i < this.m_dwRightMask.length; i++) {
			if ((dwRight & m_dwRightMask[i]) > 0 || (i == 0 && dwRight < 0x10000000)) {
				this.m_dwRight[i] = dwRight & MjConstants.MASK_CHI_HU_RIGHT;
			} else
				this.m_dwRight[i] = dwOtherRight;
		}
		return;
	}

	// 与等于 &=
	public void YuFuzhi(int dwRight) {
		boolean bNavigate = false;
		// 验证权位
		if (!IsValidRight(dwRight)) {
			// 验证取反权位
			assert (IsValidRight(~dwRight));
			if (!IsValidRight(~dwRight))
				return;
			// 调整权位
			int dwHeadRight = (~dwRight) & 0xF0000000;
			int dwTailRight = dwRight & MjConstants.MASK_CHI_HU_RIGHT;
			dwRight = dwHeadRight | dwTailRight;
			bNavigate = true;
		}

		for (byte i = 0; i < m_dwRightMask.length; i++) {
			if ((dwRight & m_dwRightMask[i]) > 0 || (i == 0 && dwRight < 0x10000000)) {
				m_dwRight[i] &= (dwRight & MjConstants.MASK_CHI_HU_RIGHT);
			} else if (!bNavigate)
				m_dwRight[i] = 0;
		}

		return;
	}

	// 或等于 |=
	public void HuoFuzhi(int dwRight) {
		// 验证权位
		if (!IsValidRight(dwRight))
			return;

		for (byte i = 0; i < m_dwRightMask.length; i++) {
			if ((dwRight & m_dwRightMask[i]) > 0 || (i == 0 && dwRight < 0x10000000)) {
				m_dwRight[i] |= (dwRight & MjConstants.MASK_CHI_HU_RIGHT);
				break;
			}
		}
		return;
	}

	// 与 &
	public CChiHuRight Yu(int dwRight) {
		CChiHuRight chr = new CChiHuRight();
		chr = this;
		chr.YuFuzhi(dwRight);
		return chr;
	}

	// 或 |
	public CChiHuRight Huo(int dwRight) {
		CChiHuRight chr = new CChiHuRight();
		chr = this;
		chr.HuoFuzhi(dwRight);
		return chr;
	}

	// 相等 ==
	public boolean Xiangdeng(int dwRight) {
		CChiHuRight chr = new CChiHuRight();
		chr.Fuzhi(dwRight);
		if (this.Xiangdeng(chr))
			return true;
		else
			return false;
	}

	// 相等 ==
	public boolean Xiangdeng(final CChiHuRight chr) {

		for (int i = 0; i < m_dwRight.length; i++) {
			if (m_dwRight[i] != chr.m_dwRight[i])
				return false;
		}
		return true;
	}

	// 不相等 ！=
	public boolean Buxiangdeng(int dwRight) {
		CChiHuRight chr = new CChiHuRight();
		chr.Fuzhi(dwRight);
		return this.Buxiangdeng(chr);
	}

	// 不等于 ！=
	public boolean Buxiangdeng(final CChiHuRight chr) {
		return !((this).Xiangdeng(chr));
	}

	// 是否权位为空
	public boolean IsEmpty() {
		for (byte i = 0; i < m_dwRight.length; i++)
			if (m_dwRight[i] > 0)
				return false;
		return true;
	}

	// 设置权位为空
	public void SetEmpty() {
		ZeroMemory(m_dwRight);
		return;
	}

	// 获取权位数值
	public byte GetRightData(int dwRight[], byte cbMaxCount) {
		assert (cbMaxCount >= m_dwRight.length);
		if (cbMaxCount < m_dwRight.length)
			return 0;

		// CopyMemory( dwRight,m_dwRight,sizeof(DWORD)*CountArray(m_dwRight) );
		System.arraycopy(m_dwRight, 0, dwRight, 0, m_dwRight.length);
		return (byte) (m_dwRight.length);
	}

	// 设置权位数值
	public boolean SetRightData(final int dwRight[], byte cbRightCount) {
		assert (cbRightCount <= m_dwRight.length);
		if (cbRightCount > m_dwRight.length)
			return false;

		ZeroMemory(m_dwRight);
		// CopyMemory( m_dwRight,dwRight,sizeof(DWORD)*cbRightCount );
		System.arraycopy(dwRight, 0, m_dwRight, 0, dwRight.length);
		return true;
	}

	// 检查权位是否正确
	private boolean IsValidRight(int dwRight) {
		int dwRightHead = dwRight & 0xF0000000;
		for (byte i = 0; i < m_dwRightMask.length; i++)
			if (m_dwRightMask[i] == dwRightHead)
				return true;
		return false;
	}

	public void ZeroMemory(int[] array) {
		for (int i = 0; i < array.length; i++) {
			array[i] = 0;
		}
	}

	public boolean isM_bInit() {
		return m_bInit;
	}

	public void setM_bInit(boolean m_bInit) {
		this.m_bInit = m_bInit;
	}

	public int[] getM_dwRightMask() {
		return m_dwRightMask;
	}

	public void setM_dwRightMask(int[] m_dwRightMask) {
		this.m_dwRightMask = m_dwRightMask;
	}

	public int[] getM_dwRight() {
		return m_dwRight;
	}

	public void setM_dwRight(int[] m_dwRight) {
		this.m_dwRight = m_dwRight;
	}

}
